package com.JFapp.controller;

import com.JFapp.domain.dto.ChangeStatusDto;
import com.JFapp.domain.dto.ChangeUserStatusDto;
import com.JFapp.domain.dto.RoleMenuIdsDto;
import com.JFapp.domain.entity.ResponseResult;
import com.JFapp.domain.entity.Role;
import com.JFapp.domain.entity.User;
import com.JFapp.domain.vo.RoleVo;
import com.JFapp.eumns.AppHttpCodeEnum;
import com.JFapp.exception.SystemException;
import com.JFapp.service.RoleService;
import com.JFapp.utils.BeanCopyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/system/role")
@Api(tags = "角色管理")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @ApiOperation("新增角色")
    @PostMapping("/roleAdd")
    public ResponseResult addRole(@RequestBody RoleMenuIdsDto roleMenuIdsDto){
        return roleService.addRole(roleMenuIdsDto);
    }

    @ApiOperation("分页查询所有角色")
    @GetMapping("/list")
    public ResponseResult selectRole(Integer pageNum,Integer pageSize){
        return roleService.selectRole(pageNum, pageSize);
    }

    @ApiOperation("查询所有角色")
    @GetMapping("/listAllRole")
    public ResponseResult selectAllRole(){
        return roleService.selectAllRole();
    }

    @GetMapping("/{roleId}")
    @ApiOperation("查询角色详细")
    public ResponseResult selectRoleById(@PathVariable Long roleId){
        return  roleService.selectRoleById(roleId);
    }


    @ApiOperation("Excel用户赋角")
    @PostMapping("/roleEmpower/UploadExcel")
    public ResponseResult empower( MultipartFile excel) throws IOException {
        String filename = excel.getOriginalFilename();
        if(!filename.endsWith(".xls")&&!filename.endsWith(".xlsx")){
            throw new SystemException(AppHttpCodeEnum.FILE_TYPE_ERROR);
        }
        return roleService.empower(excel);
    }

    @ApiOperation("用户赋角")
    @PostMapping("/roleEmpower")
    public ResponseResult empower(@RequestBody List<User> users, Long Rid){
        return roleService.empower(users,Rid);
    }

    @ApiOperation("更改角色状态")
    @PutMapping("/changeStatus")
    public ResponseResult changeStatus(@RequestBody ChangeStatusDto changeRoleStatusDto){
        return roleService.changeStatus(changeRoleStatusDto.getRoleId(), changeRoleStatusDto.getStatus());
    }

    @ApiOperation("更改角色信息")
    @PutMapping("")
    public ResponseResult changeRole(@RequestBody RoleMenuIdsDto roleMenuIdsDto){
        return  roleService.changeRole(roleMenuIdsDto);
    }


}
